//
//  GameDetailTableView.m
//  gamebox_qpp
//
//  Created by 赵隆杰 on 2022/9/3.
//

#import "GameDetailTableView.h"
#import "GameDetailTableViewCell.h"
#import "GameDetailTableViewCell_Introduction.h"
#import "GameDetailTableViewCell_Welfare.h"
#import "GameDetailTableViewCell_VIPPrice.h"
#import "GameDetailTableViewCell_Correlation.h"
#import "GameDetailTopHeaderView.h"
#import "GameDetailHeaderView.h"
#import "GameDetailTableViewCell_Bottom.h"


@interface GameDetailTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) BOOL tableCanScroll;


@end

@implementation GameDetailTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self config];
        self.detailModel = [[GameDetailNewModel alloc] init];
    }
    return self;
}
-(void)config
{
    self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.backgroundColor = [UIColor whiteColor];

    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, CGFLOAT_MIN)];
    
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.estimatedRowHeight = 0;
    
    for (NSInteger i = 0; i < 4; i++) {
        NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:i];
        [self registerNib:[UINib nibWithNibName:[self getTableCellString:indexPath0] bundle:nil] forCellReuseIdentifier:[self getTableCellString:indexPath0]];
        if (i < 3) {
            NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:i];
            [self registerNib:[UINib nibWithNibName:[self getTableCellString:indexPath1] bundle:nil] forCellReuseIdentifier:[self getTableCellString:indexPath1]];
        }
    }
    [self registerClass:[GameDetailHeaderView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([GameDetailHeaderView class])];
//    [self registerClass:[GameDetailFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([GameDetailFooterView class])];
    
    GameDetailTopHeaderView *topHeader = [[GameDetailTopHeaderView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, [GameDetailTopHeaderView height])];
    [topHeader refreshData];
    self.tableHeaderView = topHeader;
    
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.mj_header beginRefreshing];
        if (self.gamedetail_HeaderReloadBlock) {
            self.gamedetail_HeaderReloadBlock();
        }
    }];
        
    self.tableCanScroll = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollNotification:) name:@"GameDetail_tableCanScroll" object:nil];
}

- (void)reloadData
{
    [super reloadData];
    GameDetailTopHeaderView *topHeader = (GameDetailTopHeaderView *)self.tableHeaderView;
    [topHeader refreshData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [GameDetailHeaderView height];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    GameDetailHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([GameDetailHeaderView class])];
    if (!header) {
        header = [[GameDetailHeaderView alloc] initWithReuseIdentifier:NSStringFromClass([GameDetailHeaderView class])];
    }
    switch (section) {
        case 0:
            header.title = @"游戏介绍";
            break;
        case 1:
            header.title = @"上线福利";
            break;
        case 2:
            header.title = @"VIP价格表";
            break;
        case 3:
            header.title = @"相关游戏";
            break;
            
        default:
            break;
    }
    
    return header;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section < 3) {
        return 2;
    }
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    const char *className = [[self getTableCellString:indexPath] cStringUsingEncoding:NSASCIIStringEncoding];
    Class cls = objc_getClass(className);
    
    return [cls height:self.detailModel];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    GameDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getTableCellString:indexPath] forIndexPath:indexPath];
    if (indexPath.section < 3 && indexPath.row == 1) {
        GameDetailTableViewCell_Bottom *cell_Bottom = (GameDetailTableViewCell_Bottom *)cell;
        switch (indexPath.section) {
            case 0:
                [cell_Bottom initCellIsOpen:self.detailModel.isOpen_Introduce];
                break;
            case 1:
                [cell_Bottom initCellIsOpen:self.detailModel.isOpen_Fuli];
                break;
            case 2:
                [cell_Bottom initCellIsOpen:self.detailModel.isOpen_VIP];
                break;
                
            default:
                break;
        }
        cell_Bottom.footerViewBlock = ^(BOOL _isOpen) {
            switch (indexPath.section) {
                case 0:
                    self.detailModel.isOpen_Introduce = _isOpen;
                    break;
                case 1:
                    self.detailModel.isOpen_Fuli = _isOpen;
                    break;
                case 2:
                    self.detailModel.isOpen_VIP = _isOpen;
                    break;
                    
                default:
                    break;
            }
            [UIView performWithoutAnimation:^{
                [tableView reloadSection:indexPath.section withRowAnimation:UITableViewRowAnimationNone];
            }];
        };
    } else {
        [cell initCellData:self.detailModel];
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"table -- %.2f", scrollView.contentOffset.y);
    
    if (!self.tableCanScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    if (scrollView.contentOffset.y <= 0) {
        _tableCanScroll = NO;
//        NSLog(@"-*- 通知 scrollView 可滑动");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GameDetail_scrollCanScroll" object:nil userInfo:@{@"canScroll":@"1"}];
    }
    
    //使header跟随cell一起滑动
    if (scrollView == self) {
        CGFloat sectionHeaderHeight = [GameDetailHeaderView height];
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }

}

#pragma mark - NSNotificationCenter
- (void)scrollNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if (canScroll.boolValue) {
        _tableCanScroll = YES;
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GameDetail_tableCanScroll" object:nil];
}

#pragma mark - Data

- (NSString *)getTableCellString:(NSIndexPath *)indexPath
{
    NSString *classStr = @"GameDetailTableViewCell";
    switch (indexPath.section) {
        case 0:
            if (indexPath.row == 0) {
                classStr = [classStr stringByAppendingString:@"_Introduction"];
            } else if (indexPath.row == 1) {
               classStr = [classStr stringByAppendingString:@"_Bottom"];
            }
            break;
        case 1:
            if (indexPath.row == 0) {
                classStr = [classStr stringByAppendingString:@"_Welfare"];
            } else if (indexPath.row == 1) {
               classStr = [classStr stringByAppendingString:@"_Bottom"];
            }
            break;
        case 2:
            if (indexPath.row == 0) {
                classStr = [classStr stringByAppendingString:@"_VIPPrice"];
            } else if (indexPath.row == 1) {
                classStr = [classStr stringByAppendingString:@"_Bottom"];
            }
            break;
        case 3:
            classStr = [classStr stringByAppendingString:@"_Correlation"];
            break;
            
        default:
            break;
    }
    return classStr;
}




@end
